﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class Board:BoardBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }
        protected List<TopicInfo> TopicDataList = new List<TopicInfo>();
        protected List<GroupNewsInfo> NewsList = new List<GroupNewsInfo>();
        protected List<ActionInfo> ActionLatestList = new List<ActionInfo>();
        protected List<TopicInfo> TopicDataListHottest = new List<TopicInfo>();
        protected List<TopicInfo> TopicDataListLatest = new List<TopicInfo>();
        protected int n = 8;

        // 此疾病的病友实例
        protected List<UserInfo> PatientListRelated = new List<UserInfo>();

        // 论坛所对应疾病的常用治疗
        protected List<TreatmentAndUsedCount> mostFrequentUsedTreatmentsOfDisease = new List<TreatmentAndUsedCount>();

        // 论坛所对应疾病的病人按区域的分布
        protected List<ProvinceAndPatientCount> patientLocationDistributionStatOfDisease = new List<ProvinceAndPatientCount>();

        protected int PageCount;
        protected int PageIndex;
        protected int PageSize = 20;
        protected string PagingString = "";
        protected int DataCount = 0;

        protected bool ShouldNotDisplayStat = false;

        protected int IsBest;
        protected string TopicSort;
        protected TopicSearchCondition cond = new TopicSearchCondition();
        protected void HandleCommon()
        {

            //增加版块的点击次数
            GroupBLL.Instance.IncreaseGroupHits(GroupData.Id);
            NewsList = GroupNewsBLL.Instance.GetLattestNews(GroupData.Id, 10);
            ActionLatestList = ActionBLL.Instance.GetLattestHugBookList(n);
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(n);
            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(n);
            this.SetTitle(GroupData.GroupName);
          
        }

        protected void HandlePost()
        {
                //获取帖子操作的行为
                int actiontype = RequestTools.GetFormInt("actiontype", -1);
                //获取帖子所分类别
                string TopicSort = RequestTools.GetFormString("TopicSort");

                int[] idArray = Utils.StrToIntArray(RequestTools.GetFormString("topicid"), ",");

                bool succ = false;
                switch (actiontype)
                {
                    case 1://置顶帖子

                        succ = TopicBLL.Instance.UpdateTopicLevel(GroupData.Id, idArray, Constants.TopicGroupTop);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;
                    case 2: //加精帖子
                        succ = TopicBLL.Instance.UpdateBestTopic(GroupData.Id, idArray, 1);

                        if (succ)
                        {
                            UserBLL.Instance.UpdateUserCreditsByDigest(idArray);//对相应用户设置积分
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;
                    case 3: //取消置顶
                        succ = TopicBLL.Instance.UpdateTopicLevel(GroupData.Id, idArray, Constants.TopicGeneral);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            this.SetShowBackLink(true);
                            return;
                        }
                        break;
                    case 4: //取消加精
                        succ = TopicBLL.Instance.UpdateBestTopic(GroupData.Id, idArray, 0);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;
                    case 5://设置帖子标题颜色(绿色)
                        succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_Green);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;
                    case 6://设置帖子标题颜色(红色)
                        succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_Red);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;
                    case 7://取消标题颜色高亮显示
                        succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_General);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                        break;


                    case 0://删除帖子
                        foreach (int tid in idArray)
                        {
                            int uid = TopicBLL.Instance.GetTopicInfo(tid).PostUserId;
                            succ = TopicBLL.Instance.DeleteTopic(GroupData.Id, tid, uid);
                            if (!succ) break;
                        }
                        if (succ)
                        {
                            this.AddMsgLine("操作成功！");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }

                        break;
                }


                        succ = TopicBLL.Instance.UpdateTopicSort(GroupData.Id, idArray,TopicSort);
                        if (succ)
                        {
                            this.AddMsgLine("操作成功!");
                            this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                            this.SetMetaRefresh();
                        }
                        else
                        {
                            this.AddErrLine("操作失败!");
                            return;
                        }
                   
                }

        protected void HandleGet()
        {
            if (GroupData.GroupName == "站务版块")
                ShouldNotDisplayStat = true;

            TopicSort = RequestTools.GetQueryString("sort");
            IsBest = RequestTools.GetQueryInt("IsBest", -1);
            
            PageIndex = RequestTools.GetQueryInt("page", 1);
            if (TopicSort != "")
            {
                cond.Sort = TopicSort;
            }
            if (IsBest != -1)
            {
                cond.IsBest = IsBest;
            }
            TopicDataList = TopicBLL.Instance.GetTopicListByCond(GroupData.Id,cond, PageIndex,PageSize, out DataCount);


            PageCount = Utils.GetPageCount(DataCount, PageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                PageIndex,
                PageCount,
                "/BBS/Board.aspx" + query, "page");

            if (GroupData.DiseaseId <= 0)
            {
                GroupData.DiseaseId = DiseaseBLL.Instance.GetDiseaseIdByName(GroupData.GroupName);
            }

            if (GroupData.DiseaseId > 0)
            {
                // 获得患有该疾病的若干病人
                PatientListRelated = PatientBLL.Instance.GetHottestPatientByDisease(GroupData.DiseaseId, 2);

                // 获得论坛所对应疾病的常用治疗
                mostFrequentUsedTreatmentsOfDisease =
                    DiseaseBLL.Instance.GetMostFrequentUsedTreatmentsOfDisease(GroupData.DiseaseId, 4);

                // 获得论坛所对应疾病的病人按区域（省份）的分布
                patientLocationDistributionStatOfDisease =
                    DiseaseBLL.Instance.GetPatientLocationDistributionStatOfDisease(GroupData.DiseaseId, 4);
            }

        }


        //判断是否版主
        protected bool IsModerator()
        {         
                bool retVal = false;
                for (int i = 0; i < moderatorsId.Length; i++)
                {
                    if (UserData.Id == moderatorsId[i])
                    {
                        retVal = true;
                        break;
                    }
                }
                return retVal;
        }
    }
}